//
//  MTRegisterViewController.m
//  ShiShiBang
//
//  Created by 宋宝强 on 16/9/9.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTRegisterViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import "MTCountryViewController.h"
#import "MTSetPasswordViewController.h"

@interface MTRegisterViewController ()

@property (weak, nonatomic) IBOutlet UIButton *getVeriCode;
@property (weak, nonatomic) IBOutlet UITextField *numFileld;
@property (weak, nonatomic) IBOutlet UITextField *veriCodeFileld;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UIButton *countryBtn;
@property (weak, nonatomic) IBOutlet UILabel *countryCodeLable;


@end

@implementation MTRegisterViewController
- (IBAction)dismissClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
 
}

#pragma mark ------国家地区点击
- (IBAction)countryClick:(id)sender {
    MTCountryViewController *vc = [[MTCountryViewController alloc] init];
    // 监听用户的选择
    vc.areaSelectedBlock = ^(NSString *name,NSString *code){
        //刷新数据
        [self.countryBtn setTitle:[NSString stringWithFormat:@"%@",name] forState:UIControlStateNormal];
        self.countryCodeLable.text = code;
    };
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

    [self presentViewController:nav animated:YES completion:nil];

}

#pragma mark ------获取验证码
- (IBAction)getVeriCode:(id)sender {
 
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_numFileld.text zone:self.countryCodeLable.text customIdentifier:nil result:^(NSError *error){
        error = nil;
        if (!error) {
            __block int timeout= 59; //倒计时时间
            
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            
            dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
            dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0 *NSEC_PER_SEC, 0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if(timeout <= 0){ //倒计时结束，关闭
                    dispatch_source_cancel(_timer);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        [self.getVeriCode setTitle:@"获取验证码" forState:UIControlStateNormal];
                        [self.getVeriCode setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
                   
                        self.getVeriCode.userInteractionEnabled = YES;
                    });
                } else {
                    
                    int seconds = timeout % 60;
                    
                    NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        //设置界面的按钮显示 根据自己需求设置
                        
                        [UIView beginAnimations:nil context:nil];
                        
                        [UIView setAnimationDuration:1];
                        
                        [self.getVeriCode setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
                           [self.getVeriCode setTitleColor:UIColorFromRGB(0xffc000) forState:UIControlStateNormal];
                        [UIView commitAnimations];
                        
                        self.getVeriCode.userInteractionEnabled = NO;
                        
                    });
                    
                    timeout --;
                }
            });
            dispatch_resume(_timer);
        } else {
            NSLog(@"%@",error);

            [MBProgressHUD showError:@"请输入电话号码"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            });
        }
    }];
    
}

#pragma mark ------点击注册
- (IBAction)registerBtnClick:(UIButton *)sender {
   
    [MBProgressHUD showHUDWithMessage:@"正在验证中" toView:self.view];
    [SMSSDK commitVerificationCode:self.veriCodeFileld.text phoneNumber:_phoneNumber.text   zone:@"+86" result:^(NSError *error) {
       //调试阶段
        error = nil;
        if (!error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            MTSetPasswordViewController *vc = [[MTSetPasswordViewController alloc] init];
            
            vc.numberPhone = _phoneNumber.text.integerValue;

            [self presentViewController:vc animated:YES completion:nil];
//            NSLog(@"验证成功");
        }
        else
        {
            [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error.userInfo[@"commitVerificationCode"]] toView:self.view];
            NSLog(@"错误信息:%@",error);
        }
    }];
}


//监听键盘高度
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    //键盘高度
    [UIView animateWithDuration:0.1 animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, -100);
    }];
}

-(void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [UIView animateWithDuration:0.1 animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, 0);
    }];    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:0.3 animations:^{
        [self.view endEditing:YES];
    }];
}
@end
